package com.jingyanzi.finance.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.finance.dao.CashCashierMapper;
import com.jingyanzi.finance.dao.CashPlaceMapper;
import com.jingyanzi.finance.domain.CashPlaceDO;
import com.jingyanzi.finance.entity.CashPlaceExample.Criteria;
import com.jingyanzi.finance.entity.CashCashier;
import com.jingyanzi.finance.entity.CashCashierExample;
import com.jingyanzi.finance.entity.CashPlace;
import com.jingyanzi.finance.entity.CashPlaceExample;
import com.jingyanzi.finance.query.CashPlaceQueryDO;
import com.jingyanzi.finance.result.ResultDO;
import com.jingyanzi.finance.result.ResultSupport;
import com.jingyanzi.finance.service.IPlaceService;
import com.jingyanzi.finance.util.BeanUtilsExtends;


@Service("placeService")
public class PlaceServiceImpl implements IPlaceService {

	@Autowired
	private CashPlaceMapper cashPlaceMapper;
	
	@Autowired
	private CashCashierMapper cashCashierMapper;

	public ResultDO add(CashPlaceDO domain) {
		
		CashPlace entity = new CashPlace();
		
		ResultSupport result = BeanUtilsExtends.copy(entity, domain);
        if (!result.isSuccess())
        	return result;
        
        entity.setModifi(domain.getAuthor());
        entity.setCreatetime(Calendar.getInstance().getTime());
        entity.setModifitime(entity.getCreatetime());
        entity.setIsDelete(1);
        
        int r = -1;
        
        try {
            r = cashPlaceMapper.insert(entity);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        
        if (r < 1) {
        	result.setSuccess(false);
        }
 
        result.setModel(ResultDO.FIRST_MODEL_KEY, entity.getId());
        return result;
	}
	
	public ResultDO band(CashPlaceDO domain) {
		
		CashCashier entity = new CashCashier();
		
		ResultSupport result = new ResultSupport();
		
		entity.setPlaceId(domain.getId());
		entity.setEmployeeId(domain.getCashierId());
		
		int r = -1;
        
        try {
            r = cashCashierMapper.updateByPrimaryKeySelective(entity);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        
        if (r < 1) {
        	result.setSuccess(false);
        }
 
        result.setModel(ResultDO.FIRST_MODEL_KEY, entity.getId());
        return result;
	}
	
	public ResultDO unBand(CashPlaceDO domain) {
	
		ResultSupport result = new ResultSupport();
		
		CashCashierExample example = new CashCashierExample();
		
		example.createCriteria()
			.andEmployeeIdEqualTo(domain.getCashierId());
		int r = -1;
        
        try {
            r = cashCashierMapper.deleteByExample(example);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        
        if (r < 1) {
        	result.setSuccess(false);
        }
 
        return result;
	}

	public ResultDO modifi(CashPlaceDO domain) {
		
		CashPlace entity = new CashPlace();
		
		ResultSupport result = BeanUtilsExtends.copy(entity, domain);
        if (!result.isSuccess())
        	return result;
        
        int r = -1;
        
        try {
            r = cashPlaceMapper.updateByPrimaryKeySelective(entity);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        
        if (r < 1) {
        	result.setSuccess(false);
        }
        
        return result;
	}
	
	public ResultDO remove(Integer[] ids) {
		
		ResultSupport result = new ResultSupport();
		for(Integer id : ids) {
			ResultDO res = remove(id);
			if(!res.isSuccess()) {
				return res;
			}
		}
        return result;
	}

	public ResultDO remove(Integer id) {
		
		CashPlace entity = new CashPlace();
		
		ResultSupport result = new ResultSupport();
        if (!result.isSuccess())
        	return result;
        int r = -1;
        entity.setId(id);
        entity.setIsDelete(-1);
        try {
            r = cashPlaceMapper.updateByPrimaryKeySelective(entity);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        
        if (r < 1) {
        	result.setSuccess(false);
        }
        return result;
	}

	public ResultDO get(Integer id) {
		
		CashPlace entity = new CashPlace();

		ResultSupport result = new ResultSupport();
        if (!result.isSuccess())
        	return result;
        
        try {
        	entity = cashPlaceMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        
        if (entity == null) {
        	result.setSuccess(false);
        }
        
        CashPlaceDO CashPlaceDO = getCashPlaceDO(entity);
        result.setModel(ResultSupport.FIRST_MODEL_KEY, CashPlaceDO);
        return result;
	}
	
	public ResultDO getPlaceByEmployeeId(Integer id) {
		
		ResultSupport result = new ResultSupport();
		
		CashCashierExample example = new CashCashierExample();
		
		List<CashCashier> list = new ArrayList<CashCashier>();
		
		example.createCriteria()
			.andEmployeeIdEqualTo(id);
		
		try {
			list = cashCashierMapper.selectByExample(example);
        } catch (Exception e) {
        	e.printStackTrace();
        }
		
		if(list.size() == 0) {
			result.setModel(ResultSupport.FIRST_MODEL_KEY, null);
			return result;
		}
		
		ResultDO res = get(list.get(0).getPlaceId());
		
		if(!res.isSuccess()) {
			result.setSuccess(false);
			return result;
		}
		return res;
	}

	public ResultDO query(CashPlaceQueryDO query) {
		
		ResultSupport result = new ResultSupport();

		List<CashPlace> list = null;

		CashPlaceExample example = new CashPlaceExample();
		
		Criteria createCriteria = example.createCriteria();
		
		if(StringUtils.isNotEmpty(query.getPlaceName())) {
			createCriteria.andPlaceNameLike("%" + query.getPlaceName() + "%");
		}
		if(query.getProvince()!=null) {
			createCriteria.andProvinceEqualTo(query.getProvince());
		}
		if(query.getCity()!=null) {
			createCriteria.andCityEqualTo(query.getCity());
		}
		if(query.getDistrict()!=null) {
			createCriteria.andDistrictEqualTo(query.getDistrict());
		}
		
		
		createCriteria.andIsDeleteEqualTo(1);
		if (query.isPage()) {
			example.setLimitByPage(query.getStart() + "," + query.getPageRows());
			long count = cashPlaceMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}
		
		List<CashPlaceDO> doList = new ArrayList<CashPlaceDO>();

		try {
			list = cashPlaceMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}

		doList = getCashPlaceDOList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, doList);

		return result;
	}
	
	public CashPlaceDO getCashPlaceDO(CashPlace entity) {
		if (entity == null)
			return null;
		CashPlaceDO dst = new CashPlaceDO();
		return BeanUtilsExtends.copyProperties(dst, entity) ? dst : null;
	}
	
	public List<CashPlaceDO> getCashPlaceDOList(List<CashPlace> list) {
		List<CashPlaceDO> result = new ArrayList<CashPlaceDO>();
		if (list != null && !list.isEmpty()) {
			for (CashPlace org : list) {
				CashPlaceDO org1 = getCashPlaceDO(org);
				if (org1 != null) {
					result.add(org1);
				}
			}
		}
		return result;
	}

}
